//
//  SetUpPswVC.m
//  hnGovernment
//
//  Created by 安仔 on 17/3/3.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "SetUpPswVC.h"
#import "SetUpPswView.h"
#import "DDbaseNetWork+Login.h"
#import "DDcacheManager.h"
#import "loginVM.h"

@interface SetUpPswVC ()<SetUpPswViewDelegate>

@property (nonatomic, strong) SetUpPswView *setUpPswView;

@end

@implementation SetUpPswVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = NSLocalString(@"设置密码");
    
    self.setUpPswView = [[SetUpPswView alloc] initWithFrame:CGRectMake(0, 120 * m6Scale, KScreenWidth, 440 * m6Scale)];
    self.setUpPswView.delegate = self;
    [self.view addSubview:self.setUpPswView];
}

- (void)clickSureBtn {
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"注册中"];
    [DDNetWorkManager registerUseMobile:self.mobile password:self.setUpPswView.firstTF.text smsCode:self.smsCode controller:self success:^(id result, DDcacheType cacheType, DDresultType resultType) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            int resultCode = [result[@"resultCode"]intValue];
            if (resultCode == 0) {
                [weakSelf login];
            }else {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD showErrorWithStatus:@"注册失败"];
                });
            }
        });
    } failure:^(NSError *error) {
       dispatch_async(dispatch_get_main_queue(), ^{
           [SVProgressHUD dismiss];
           [SVProgressHUD showSuccessWithStatus:@"注册失败"];
       });
    }];
}

- (void)login {
    WEAKSELF;
    [loginVM loginWithLoginName:self.mobile password:self.setUpPswView.firstTF.text controller:self success:^(id result, DDcacheType cacheType, DDresultType resultType) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [cacheManager cacheUserInfo:result[@"data"]];
            [SVProgressHUD dismiss];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD showSuccessWithStatus:@"注册成功"];
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            });
        });
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
